/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import database.ProductDAO;
import database.ProductManagerDAO;
import database.ReceipingDAO;
import database.StaffAuthenticationDAO;
import database.StaffDAO;
import database.SupplierDAO;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;



/**
 *
 * @author Trang
 */
@Namespace("/Administration/Receipt")
public class AddReceipAction extends ActionSupport implements ModelDriven<List<Receiptitem>>{
    private List<Receiptitem> receiptItemList;
    private Map<Integer, String> allProducts;
    
    private Receipt receipt;
    private String createDate;
    public ProductManagerDAO productDAO = new ProductManagerDAO();
    public ReceipingDAO receiptDAO = new ReceipingDAO();
    
    private Integer idStaff;
    private Map<Integer, String> allSuppliers;
    private Map<Integer, String> allStaffs;
    private List<Receipt> receips;

    public List<Receipt> getReceips() {
        return receips;
    }

    public void setReceips(List<Receipt> receips) {
        this.receips = receips;
    }
    public AddReceipAction()
    {
        allProducts = new HashMap<Integer, String>();

        
        for (Product c:productDAO.GetAllProduct()) {
            allProducts.put(c.getIdProduct(), c.getName());
        }
        
        allSuppliers = new HashMap<Integer, String>();
        for(Supplier c: supplierDAO.getAllSuppliers())
        {
            allSuppliers.put(c.getIdSupplier(), c.getName());
        }
        
        allStaffs = new HashMap<Integer, String>();
        for(Staff c: staffDAO.getAllStaff())
        {
            allStaffs.put(c.getIdStaff(), c.getFirstname()+" "+c.getLastname());
        }
    }
    public Map<Integer, String> getAllSuppliers() {
        return allSuppliers;
    }

    public void setAllSuppliers(Map<Integer, String> allSuppliers) {
        this.allSuppliers = allSuppliers;
    }

    public Map<Integer, String> getAllStaffs() {
        return allStaffs;
    }

    public void setAllStaffs(Map<Integer, String> allStaffs) {
        this.allStaffs = allStaffs;
    }
    public Integer getIdStaff() {
        return idStaff;
    }

    public void setIdStaff(Integer idStaff) {
        this.idStaff = idStaff;
    }

    public Integer getIdSupplier() {
        return idSupplier;
    }

    public void setIdSupplier(Integer idSupplier) {
        this.idSupplier = idSupplier;
    }
    private Integer idSupplier;

    
    public ReceipingDAO receipDAO = new ReceipingDAO();
    public SupplierDAO supplierDAO = new SupplierDAO();
    public StaffDAO staffDAO = new StaffDAO();
   
    @Action(value = "create-recept", results = {
        @Result(name = "success", location = "/manageReceipt/w_receip.jsp"),
        @Result(name = "input", location = "/manageReceipt/w_receipting.jsp")
    })
    public String AddReceiptView() throws Exception {
         SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
         Map session = ActionContext.getContext().getSession();
         List<Receiptitem> temp = new ArrayList<Receiptitem>();
         for (Receiptitem item : receiptItemList)
         {
             if(item.getReceiptQuantity() != 0)
                 temp.add(item);
         }
         for(Receiptitem item: temp)
         {
             Product product = new Product();
             ProductDAO productDao = new ProductDAO();
             product = productDao.getProductFromProductId(item.getProduct().getIdProduct());
             
             item.setProduct(product);
         }          
         System.out.println(idStaff);
         Date start = dateFormat.parse(createDate);
         receipDAO.addReceip(temp, idStaff, idSupplier, start);
         
         //Add staff history
         Staff staff = (Staff) session.get("staff");
            new StaffAuthenticationDAO().addAction("add receip", staff.getIdStaff());
         receips = receipDAO.getAllReceip();
         System.out.println("Trang");

        return SUCCESS;
    }
//    @Action(value = "AddReceipt", results = {
//        @Result(name = "success", location = "/w_receip.jsp") 
//            
//    })
//     public String AddNewRecept()
//     {  
//         System.out.println("Hehe");
//         System.out.println(createDate);     
//         List<Receiptitem> temp = new ArrayList<Receiptitem>();
//         for (Receiptitem item : receiptItemList)
//         {
//             if(item.getReceiptQuantity() != 0)
//                 temp.add(item);
//         }
//         for(Receiptitem item: temp)
//         {
//             Product product = new Product();
//             ProductDAO productDao = new ProductDAO();
//             product = productDao.getProductFromProductId(item.getProduct().getIdProduct());
//             item.setProduct(product);
//         }  
//         System.out.println(idSupplier);
//         
//         Supplier sup = supplierDAO.getSupplierFromSupplierID(idSupplier);
//         
//         Staff sta = staffDAO.getStaffByStaffId(idStaff);
//         receipt = new Receipt(temp, sup, sta, createDate);
//         System.out.println("Trang");
//         return SUCCESS;
//         //else return INPUT;
//     }

    /**
     * @return the createDate
     */
    public String getCreateDate() {
        return createDate;
    }

    /**
     * @param createDate the createDate to set
     */
    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }

    /**
     * @return the receiptItemList
     */
    public List<Receiptitem> getReceiptItemList() {
        return receiptItemList;
    }

    /**
     * @param receiptItemList the receiptItemList to set
     */
    public void setReceiptItemList(List<Receiptitem> receiptItemList) {
        this.receiptItemList = receiptItemList;
    }

    @Override
    public List<Receiptitem> getModel() {
        return receiptItemList; //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * @return the allProducts
     */
    public Map<Integer, String> getAllProducts() {
        return allProducts;
    }

    /**
     * @param allProducts the allProducts to set
     */
    public void setAllProducts(Map<Integer, String> allProducts) {
        this.allProducts = allProducts;
    }

    /**
     * @return the receipt
     */
    public Receipt getReceipt() {
        return receipt;
    }

    /**
     * @param receipt the receipt to set
     */
    public void setReceipt(Receipt receipt) {
        this.receipt = receipt;
    } 
}
